package org.apache.dubbo.demo.consumer.example.eg2;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.demo.example.eg2.DemoService;
import org.junit.Test;

public class Application {

  @Test
  public void connectWithInterface() {
    ApplicationConfig applicationConfig = new ApplicationConfig();
    applicationConfig.setName("demo-consumer");

    RegistryConfig registryConfig = new RegistryConfig();
    registryConfig.setAddress("zookeeper://127.0.0.1:2181");

    ReferenceConfig<DemoService> referenceConfig = new ReferenceConfig<>();
    referenceConfig.setApplication(applicationConfig);
    referenceConfig.setRegistry(registryConfig);
    referenceConfig.setInterface(DemoService.class);
    referenceConfig.setVersion("1.0.0");

    DemoService demoService = referenceConfig.get();
    String result = demoService.sayHello("xufeng");
    System.out.println("************************* result: " + result + " *************************");
  }

  /**
   * 点对点直连
   */
  @Test
  public void connectWithUrl() {
    ApplicationConfig applicationConfig = new ApplicationConfig();
    applicationConfig.setName("demo-consumer");

    ReferenceConfig<DemoService> referenceConfig = new ReferenceConfig<>();
    referenceConfig.setApplication(applicationConfig);
    referenceConfig.setUrl("dubbo://127.0.0.1:20880/org.apache.dubbo.demo.example.eg2.DemoService");
    referenceConfig.setInterface(DemoService.class);
    referenceConfig.setVersion("1.0.0");

    DemoService demoService = referenceConfig.get();
    String result = demoService.sayHello("xufeng");
    System.out.println("************************* result: " + result + " *************************");
  }

}
